package com.itsu.service.Impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itsu.dao.QuestionItemDao;
import com.itsu.domain.QuestionItem;
import com.itsu.service.QuestionItemService;
import com.itsu.utils.MapperUtils;

import java.util.List;

public class QuestionItemServiceImpl implements QuestionItemService {

    private QuestionItemDao mapper;

    @Override
    public void save(QuestionItem questionItem) {
        try {
            mapper = MapperUtils.getMapper(QuestionItemDao.class);
            mapper.save(questionItem);
            MapperUtils.commit();
        } catch (Exception e) {
            MapperUtils.rollback();
            e.printStackTrace();
        } finally {
            MapperUtils.close();
        }

    }

    @Override
    public void delete(QuestionItem questionItem) {
        try {
            mapper = MapperUtils.getMapper(QuestionItemDao.class);
            mapper.delete(questionItem);
            MapperUtils.commit();
        } catch (Exception e) {
            MapperUtils.rollback();
            e.printStackTrace();
        } finally {
            MapperUtils.close();
        }
    }

    @Override
    public void update(QuestionItem questionItem) {
        try {
            mapper = MapperUtils.getMapper(QuestionItemDao.class);
            mapper.update(questionItem);
            MapperUtils.commit();
        } catch (Exception e) {
            MapperUtils.rollback();
            e.printStackTrace();
        } finally {
            MapperUtils.close();
        }
    }

    @Override
    public QuestionItem findById(String id) {
        try {
            mapper = MapperUtils.getMapper(QuestionItemDao.class);
            QuestionItem questionItem = mapper.findById(id);
            return questionItem;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            MapperUtils.close();
        }
        return null;
    }

    @Override
    public List<QuestionItem> findAll() {
        return null;
    }

    @Override
    public List<QuestionItem> findByQuestionId(String id) {
        try {
            mapper = MapperUtils.getMapper(QuestionItemDao.class);
            List<QuestionItem> byQuestionId = mapper.findByQuestionId(id);
            return byQuestionId;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            MapperUtils.close();
        }
        return null;
    }

    @Override
    public PageInfo<QuestionItem> findByQuestionId(int pageNum, int pageSize, String questionId) {
        try {
            mapper = MapperUtils.getMapper(QuestionItemDao.class);
            PageHelper.startPage(pageNum, pageSize);
            List<QuestionItem> byQuestionId = mapper.findByQuestionId(questionId);
            PageInfo<QuestionItem> pageInfo = new PageInfo<>(byQuestionId);
            return pageInfo;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            MapperUtils.close();
        }
        return null;
    }
}
